package com.sailors.common.task.provider.faced.controller;

import com.sailors.common.task.provider.application.aggregation.IFileTaskAggregation;
import com.sailors.common.task.provider.domian.service.IFileTaskService;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskCreateExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskFailedExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.execute.RefreshProcessExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.query.WebFileTaskQueryDto;
import com.sailors.common.task.provider.infra.design.common.rest.BasePageResponse;
import com.sailors.common.task.provider.infra.design.common.rest.BaseResponse;
import com.sailors.common.task.provider.infra.design.common.rest.Bru;
import com.sailors.common.task.provider.infra.design.common.rest.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sailors.common.task.provider.faced.model.vo.*;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 文件任务管理
 *
 * @description: 文件任务管理
 * @author: cyj
 * @date 2022 -11-22 17:37:52
 */
@Slf4j
@RequestMapping("/web/file/task")
@RestController
@RefreshScope
@Api(tags = "文件任务管理")
public class WebFileTaskController {


    @Autowired
    private IFileTaskAggregation fileTaskAggregation;

    @Autowired
    private IFileTaskService fileTaskService;

    @Value("${file.task.bussiness:}")
    private String[] fileTaskBussiness;

    /**
     * 分页查询
     *
     * @param webFileTaskQueryDto web file task query dto
     * @return the base page response
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public BasePageResponse<FileTaskLogDetailVo> page(@RequestBody WebFileTaskQueryDto webFileTaskQueryDto) {
        return PageUtils.pageSuccess(fileTaskAggregation.page(webFileTaskQueryDto));
    }

    /**
     * 单个详情查询
     *
     * @param id id
     * @return the base response
     */
    @GetMapping
    @ApiOperation("单个详情查询")
    public BaseResponse<FileTaskLogDetailVo> get(@RequestParam(value = "id") Long id) {
        return Bru.success(fileTaskAggregation.get(id));
    }

    /**
     * 当前进度查询--适用页面刷新
     *
     * @param ids ids
     * @return the base response
     */
    @PostMapping("/nowProcess")
    @ApiOperation("当前进度查询--适用页面刷新")
    public BaseResponse<List<FileTaskLogDetailVo>> nowProcess(@RequestBody List<Long> ids) {
        return Bru.success(fileTaskAggregation.nowProcess(ids));

    }

    /**
     * 所有任务类型下拉框
     *
     * @return the base response
     */
    @GetMapping("/bussiness")
    @ApiOperation("所有任务类型下拉框")
    public BaseResponse<List<String>> bussiness() {
        return Bru.success(fileTaskAggregation.bussiness());
    }

    /**
     * 刷新进度
     *
     * @param refreshProcessExecuteDto refresh process execute dto
     * @return the base response
     */
    @PutMapping("/refresh/process")
    @ApiOperation("刷新进度")
    public BaseResponse refreshProcess(@RequestBody @Validated RefreshProcessExecuteDto refreshProcessExecuteDto) {
        fileTaskAggregation.refreshProcess(refreshProcessExecuteDto);
        return Bru.success();
    }

    /**
     * 任务失败更新
     *
     * @param fileTaskFailedExecuteDto file task failed execute dto
     * @return the base response
     */
    @PutMapping("/failed")
    @ApiOperation("任务失败更新")
    public BaseResponse failed(@RequestBody FileTaskFailedExecuteDto fileTaskFailedExecuteDto) throws ExecutionException, InterruptedException {
        fileTaskAggregation.failed(fileTaskFailedExecuteDto);
        return Bru.success();
    }

    /**
     * 创建任务
     *
     * @param fileTaskCreateExecuteDto file task create execute dto
     * @return the base response
     */
    @PostMapping
    @ApiOperation("创建任务")
    public BaseResponse<Long> create(@RequestBody @Validated FileTaskCreateExecuteDto fileTaskCreateExecuteDto) throws ExecutionException, InterruptedException {
        return Bru.success(fileTaskAggregation.create(fileTaskCreateExecuteDto));
    }

}
